package com.ddwl.gate.service;

import com.ddwl.common.constant.DingTalkEnum;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.gate.dao.DingTalkRecordDao;
import com.ddwl.gate.dao.DingTalkTemplateUserDao;
import com.ddwl.schema.bo.gate.QueryDingTalkTemplateBo;
import com.ddwl.schema.bo.gate.DingTalkTemplateUserDto;
import com.ddwl.gate.model.DingTalkRecord;
import com.ddwl.gate.util.RobotHelperUtil;
import com.ddwl.schema.bo.gate.DingTalkBo;
import com.dingtalk.api.response.OapiDepartmentListResponse;
import com.dingtalk.api.response.OapiUserSimplelistResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wenbin
 * @Date 2020/10/14 14:23
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DingTalkService {

    private final DingTalkRecordDao dingTalkRecordDao;
    private final DingTalkTemplateUserDao dingTalkTemplateUserDao;

    /**
     * 推送消息(机器人)
     */
    public void sendMessage(String message, String mobiles) {
        RobotHelperUtil.sendDingMessage(message, mobiles);
    }

    /**
     * 推送消息(按部门)
     */
    public void sendMessageDept(String message, List<String> department) throws Exception {
        //查询所有部门
        List<OapiDepartmentListResponse.Department> deptList = RobotHelperUtil.getDeptList();

        if (!CollectionUtils.isEmpty(department) && !CollectionUtils.isEmpty(deptList)) {
            for (String reqDep : department) {
                for (OapiDepartmentListResponse.Department dep : deptList) {
                    if (dep.getName().equals(DingTalkEnum.department.getNameByCode(Integer.valueOf(reqDep)))) {
                        //查询请求部门下面的的所有员工
                        List<OapiUserSimplelistResponse.Userlist> departmentUser = RobotHelperUtil.getDepartmentUser(dep.getId());
                        if (!CollectionUtils.isEmpty(departmentUser)) {
                            StringBuffer user = new StringBuffer("");
                            departmentUser.stream().forEach(talkUserVo -> {
                                user.append(talkUserVo.getUserid()).append(",");
                            });
                            //发消息
                            RobotHelperUtil.sendMsg(user.toString().substring(0, user.length() - 1), message, "");
                        }
                        break;
                    }
                }
            }
        }
    }

    /**
     * 推送消息(按手机号)
     */
    public void sendMessageMobile(String message, List<String> mobiles) throws Exception {

        if (CollectionUtils.isEmpty(mobiles)) {
            return;
        }
        StringBuffer ids = new StringBuffer();
        StringBuffer mobile = new StringBuffer();
        mobiles.stream().forEach(o -> {
            String id = RobotHelperUtil.getUserByMobile(o);
            if (StringUtils.hasText(id)) {
                mobile.append(o).append(",");
                ids.append(id).append(",");
            }
        });

        //发消息
        RobotHelperUtil.sendMsg(ids.toString().substring(0, ids.length() - 1), message, mobile.substring(0, mobile.length() - 1));
    }

    /**
     * 即时推送消息
     */
    public void sendMessageNow(DingTalkBo pagebo) throws Exception {
        if (!StringUtils.hasText(pagebo.getChannel())) {
            return;
        }
        QueryDingTalkTemplateBo bo = new QueryDingTalkTemplateBo();
        bo.setEffect(1);
        bo.setTemplateChannel(pagebo.getChannel());
        List<DingTalkTemplateUserDto> userList = dingTalkTemplateUserDao.getDingTalkTemplateUserList(bo);

        StringBuffer ids = new StringBuffer();
        StringBuffer mobiles = new StringBuffer();
        String message = pagebo.getMessage();
        for (DingTalkTemplateUserDto o : userList) {
            String id = RobotHelperUtil.getUserByMobile(o.getUserTel());
            if (StringUtils.hasText(id)) {
                ids.append(id).append(",");
                mobiles.append(o.getUserTel()).append(",");
                if (!StringUtils.hasText(message)) {
                    message = o.getTemplate();
                }
            }
        }
        //发消息
        RobotHelperUtil.sendMsg(ids.toString().substring(0, ids.length() - 1), message, mobiles.substring(0, mobiles.length() - 1));
    }

    public void changeId() {
        List<DingTalkRecord> list = dingTalkRecordDao.getAll();
        if (!CollectionUtils.isEmpty(list)) {
            Map<String, String> map = new HashMap<>();
            for (DingTalkRecord dtr : list) {
                String id = dtr.getId();
                if (id.length() == 11) {
                    long longId = UUIDGenerator.reverseShortUUID(id);
                    map.put(id, String.valueOf(longId));
                }
            }
            if (!CollectionUtils.isEmpty(map)) {
                dingTalkRecordDao.changeId(map);
            }
        }
    }
}
